package cn.hinglo.common.handler;

import com.jfinal.handler.Handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @createTime: 2018/7/24
 * @author: HingLo
 * @description: options请求过滤, 之所以过滤options 在开发者模式下少打印日志，正式环境中少走一次拦截器流程
 */
public class OptionsHandler extends Handler {
    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setCharacterEncoding("utf-8");
            PrintWriter out;
            try {
                out = response.getWriter();
                out.println();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            next.handle(target, request, response, isHandled);
        }
    }
}
